package UnitTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Auther: dingtian
 * @Date: 20-2-16 15 37
 * @Description:
 * 给定x台机器的集群，处理文件。不同类型文件需要的服务器数目不一样。
 * 表示为文件大小/需要服务器数目 ，给出机器台数x，求并行处理文件数
 */
public class ClusterDealFile {

    public static void main(String[] args) {
        Number task1 = new Number("300/20");
        Number task2 = new Number("500/30");
        Number task3 = new Number("620/50");
        Number task4 = new Number("370/30");
        Number task5 = new Number("400/50");
        Number task6 = new Number("450/30");
        Number task7 = new Number("380/40");
        Number task8 = new Number("150/10");
        List<Number> numbers = Arrays.asList(task1, task2, task3, task4, task5, task6, task7, task8);
        int x = 50;
        ClusterDealFile dealFile = new ClusterDealFile();
        int i = dealFile.calDealFileNum(x, numbers);
        System.out.println(i);
    }

    private int calDealFileNum(int x,List<Number> numbers){
        AtomicInteger res = new AtomicInteger(0);
        List<Number> numberList = numbers.stream().sorted(Comparator.comparing(Number::getDenominator)).collect(Collectors.toList());
        helper(res,x,numberList,new ArrayList<>(),0);
        return res.intValue();
    }


    private void helper(AtomicInteger result, int x, List<Number> numbers, List<Number> targetNum, int index) {
        if (x <= 0) {
            int temp = targetNum.stream().map(Number::getNumerator).mapToInt(Integer::intValue).sum();
            if (x < 0) {
                Number number = targetNum.get(targetNum.size() - 1);
                int temp2 = temp - number.getNumerator();
                if (temp2 > result.intValue()) {
                    result.set(temp2);
                }
            } else {
                if (temp > result.intValue()) {
                    result.set(temp);
                }
            }
            return;
        }
        for (int i = index; i < numbers.size(); i++) {
            if (i > index && numbers.get(i).getDenominator().equals(numbers.get(i - 1).getDenominator())) {
                continue;
            }
            targetNum.add(numbers.get(i));
            helper(result, x - numbers.get(i).getDenominator(), numbers, targetNum, i + 1);
            targetNum.remove(targetNum.size() - 1);
        }
    }
}

class Number{
    /**
     * 分子
     */
    private Integer numerator;
    /**
     * 分母
     */
    private Integer denominator;

    public Integer getNumerator() {
        return numerator;
    }

    public void setNumerator(Integer numerator) {
        this.numerator = numerator;
    }

    public Integer getDenominator() {
        return denominator;
    }

    public void setDenominator(Integer denominator) {
        this.denominator = denominator;
    }

    public Number(String express) {
        String[] split = express.split("/");
        this.numerator = Integer.valueOf(split[0]);
        this.denominator = Integer.valueOf(split[1]);
    }
}
